import java.util.Arrays;

/**
 * @author JiuQi 玖柒
 * @date 2021/7/7 21:23
 * 双指针  做笔记
 */
public class T283 {
    public static void main(String[] args) {
        int[] nums = {0, 1, 2, 0, 12};
        new Solution5().moveZeroes(nums);
    }
}

class Solution5 {
    public void moveZeroes(int[] nums) {
        int n = nums.length, left = 0, right = 0;
        while (right<n) {
            if (nums[right] != 0) {
                //交换值  若nums[0] != 0 因为left和right初值都为0,所以也可进行交换
                int temp = nums[right];
                nums[right] = nums[left];
                nums[left] = temp;
                left++;
            }
            right++;
        }
        System.out.println(Arrays.toString(nums));
    }
}

//不使用双指针
/*class Solution5 {
    public void moveZeroes(int[] nums) {
        System.out.println("原数组:"+Arrays.toString(nums));
        int n = nums.length, index = 0;
        for (int i = 0; i < n; i++) {
            //不为0直接赋值
            if (nums[i] != 0) {
                nums[index++] = nums[i];
            }
        }
        //n - index 即为0出现的次数
        for (int i = index; i < n; i++) {
            nums[i] = 0;
        }
        System.out.println("操作后的数组"+Arrays.toString(nums));
    }
}*/


































